from py_stealth import *


def PickupArrow():
    SetFindDistance(2)
    while FindType(0x0F3F, Ground()) > 0:
        Grab(FindItem(), FindQuantity())
        CheckLag(3000)
        Wait(500)


def UseSkill():
    if GetMana(Self()) > 10 and not IsActiveSpellAbility('Evasion'):
        Cast('Evasion')
        CheckLag(3000)
        Wait(1000)


def GetEnemy():
    target_id = None
    target_distance = 0
    SetFindDistance(16)
    FindType(0xFFFFFFFF, Ground())
    for found in GetFindedList():
        if GetNotoriety(found) == 3:
            if target_id:
                found_distance = abs(GetX(found) - GetX(Self())) + abs(GetY(found) - GetY(Self()))
                #AddToSystemJournal(GetTooltip(found)+ str(found_distance)+' '+str(target_distance))
                if found_distance < target_distance:
                    target_id = found
                    target_distance = found_distance
            else:
                target_id = found
                target_distance = abs(GetX(found) - GetX(Self())) + abs(GetY(found) - GetY(Self()))
    return target_id


def AttackEnemy(target):
    #AddToSystemJournal('AttackEnemy:' + GetTooltip(target))
    if not WarMode():
        SetWarMode(True)
    Attack(target)


def CheckWearpon():
    if ObjAtLayer(RhandLayer()) == 0:
        if FindType(0x13B2, Backpack()) > 0:
            Equip(RhandLayer(), FindItem())
            Wait(1000)


while GetSkillValue('Archery') < 120:
    CheckWearpon()
    UseSkill()
    AttackEnemy(GetEnemy())
    PickupArrow()